package com.fingard.dsp.bank.directbank.cib03;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.common.utils.json.JSONUtils;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.text.StringHelper;

import java.util.Date;
import java.util.HashMap;

public class CIB03Balance extends CIB03Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            while (p_req7506.readRecord()) {
                ActSetItem actItem = getBankConfig().getActSetItem(p_req7506.readedDetail.actNum, "balance");
                if (actItem != null) {
                    HashMap<String, String> postMapParams = new HashMap<String, String>();
                    postMapParams.put("version", "1.0.2");
                    postMapParams.put("mchtId", actItem.merchantId);
                    postMapParams.put("signType", "RSA");
                    postMapParams.put("serialNo", p_req7506.reqHeader.transSeqID);
                    postMapParams.put("transTime", Format.DateTime14Format.format(new Date()));
                    postMapParams.put("mac", generateMAC(postMapParams, actItem));

                    String tmpStrRet = sendDataToBank(JSONUtils.map2JSON(postMapParams), "downloadSatement");
                    if (StringHelper.hasAnyChar(tmpStrRet)) {
                        if (tmpStrRet.indexOf("{") >= 0) {
                            JSONObject tmpJsonRet = JSONObject.parseObject(tmpStrRet);
                            String tmpRetCode = tmpJsonRet.getString("respCode");
                            if ("E0000".equals(tmpRetCode)) {
                                JSONArray data = tmpJsonRet.getJSONArray("data");
                                if (data.size() > 0) {
                                    for (int i = 0; i < data.size(); i++) {
                                        JSONObject detailJson = data.getJSONObject(i);
                                        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                        tmpRetDetail.currBal = detailJson.getString("balance");
                                        tmpRetDetail.availBal = detailJson.getString("balance");
                                        tmpRetDetail.actNum = detailJson.getString("acctNo");
                                        String status = detailJson.getString("status");
                                        if ("1".equals(status)) {
                                            tmpRetDetail.acState = "2";
                                        }
                                        tmpRetDetail.respCode = "0";
                                        tmpRetDetail.respInfo = tmpJsonRet.getString("respMsg");
                                        retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                                    }
                                }
                            }
                            retAts.writeRespCode("0");
                            retAts.writeRespInfo(tmpJsonRet.getString("respMsg"));
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(tmpStrRet);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpStrRet);
                    }
                }
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
